package com.lucian.ge.service.shop.impl;

import com.alibaba.fastjson.JSON;
import com.lucian.ge.dao.shop.CartMapper;
import com.lucian.ge.service.shop.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by Lucicol on 2019/6/24.
 */
@Service("CartService")
public class CartServicelmpl implements CartService {

    @Autowired(required=false)
    private CartMapper cartMapper;

    public void modifyCartQuantity(Integer userId,Integer productId,Integer count) {
        cartMapper.updateQuantity(userId,productId,count);
    }

    public void delete(Integer userId,Integer productId) {
        cartMapper.delete(userId,productId);
    }

    public void add(Integer userId,Integer productId,Integer count) {
        Map<String, String> cart = cartMapper.selectCartByUserIdProductId(userId,productId);

        if (cart == null) {
            cartMapper.add(userId,productId,count);
        } else {

            Integer quantity = Integer.parseInt(String.valueOf(cart.get("quantity"))) + count;

//            cart.put("quantity", quantity.toString());
//            String a = JSON.toJSONString(cart.get("quantity"));
//            System.out.println(a);
            cartMapper.updateQuantity(userId,productId,quantity);
        }

    }

    public List<Map<String, String>> getCartList(Integer userId) {
        List<Map<String, String>> cartList = cartMapper.selectList(userId);
//        System.out.println(productList);
        return cartList;
    }
}
